import { Component } from '@angular/core';
import {App, IonicPage, NavController, NavParams} from 'ionic-angular';
import {ModificationPhonePage} from "../modification-phone/modification-phone";
import {ROUTER} from "../../service/router.service";
import {LocalStorage} from "../../utils/localstorage.utils";
import {UserModel} from "../../model/user.model";
import {USER} from "../../service/user.service";
import {WechatUtil} from "../../utils/wechat.util";
import {ImageApiService} from "../../api/image-api.service";
import {USERAPI} from "../../api/user.api.service";

/**
 * Generated class for the MyInformationPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-my-information-page',
  templateUrl: 'my-information-page.html',
})
export class MyInformationPage {

  user:UserModel
  private idCard:any;
  private phone:any;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public app:App,
              private ROUTER: ROUTER,
              private imageApi:ImageApiService,
              private ls:LocalStorage,
              public WX:WechatUtil,
              private USERAPI:USERAPI,
              private USER:USER
  ) {
    this.user = this.ls.getObject('user') as UserModel;
    console.log(this.user);
    this.idCard = this.user.idcard!=null?this.user.idcard.replace(/^(.{4})(?:\d+)(.{1})$/,"$1*************$2"):'无';
    this.phone = this.user.username!=null?this.user.username.replace(/^(.{3})(?:\d+)(.{3})$/,"$1*******$2"):'无';
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MyInformationPage');
  }

  uploadIdcard(key:string){
      this.WX.chooseAndUploadImage(1,v=>{
      },mediaId=>{
        this.imageApi.wxImageUpload2oss(mediaId,(res:any)=>{
          this.user[key] = res.saveUrl
          this.USERAPI.updateUser(this.user,v=>{
            this.USER.toast("资料已更新")
          })
        })
      })
  }

  gotoModificationPhone() {
    this.USER.toast('程序猿哥哥正在努力建设中,很快就会好了……');
    // this.ROUTER.go(ROUTER.MODIFICATIONPHONE);
  }
  ionViewDidEnter() {
    document.title='我的资料';
  }
}
